package cn.hzw.supermarket.activity;

import static cn.hzw.supermarket.util.HttpUtil.sendHttpRequest;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import cn.hzw.supermarket.R;
import cn.hzw.supermarket.databinding.ActivityUserInfoBinding;
import cn.hzw.supermarket.databinding.FragmentMeBinding;
import cn.hzw.supermarket.domain.User;
import cn.hzw.supermarket.fragment.MeFragment;
import cn.hzw.supermarket.model.ResultInfo;

public class UserInfoActivity extends AppCompatActivity {
    private ActivityUserInfoBinding binding;
    public static User user=new User();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏，隐藏标题
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        binding = ActivityUserInfoBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initView();
    }

    private void initView() {
        binding.tvUserName.setText(LoginActivity.user.getUsername());
        binding.layoutTitle.tvTitle.setText("个人中心");


        binding.layoutTitle.ivBack.setOnClickListener(view -> {
            if (UpdateUsernameActivity.user.getUsername()!=null){
                LoginActivity.user.setUsername(UpdateUsernameActivity.user.getUsername());
                Intent intent = new Intent(this,MainActivity.class);
                startActivity(intent);
                UserInfoActivity.this.finish();
            }else {
                finish();
            }
        });
        binding.tvPhone.setText(LoginActivity.user.getPhone());
        binding.tvSex.setText(LoginActivity.user.getGender());
        binding.tvEmail.setText(LoginActivity.user.getEmail());
        binding.rlAccount.setOnClickListener(view -> {
            String username = binding.tvUserName.getText().toString();
            Bundle bundle = new Bundle();
            bundle.putString("content",username);//传递界面上的用户名数据
            Intent intent = new Intent(this,UpdateUsernameActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
            UserInfoActivity.this.finish();
        });
        binding.rlPhone.setOnClickListener(view -> {
            String phone = binding.tvPhone.getText().toString();
            Bundle bundle = new Bundle();
            bundle.putString("phone",phone);//传递界面上的手机数据
            Intent intent = new Intent(this,UpdatePhoneActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
            UserInfoActivity.this.finish();
        });
        binding.rlEmail.setOnClickListener(view -> {
            String email = binding.tvEmail.getText().toString();
            Bundle bundle = new Bundle();
            bundle.putString("email",email);//传递界面上的邮箱数据
            Intent intent = new Intent(this,UpdateEmailActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
            UserInfoActivity.this.finish();
        });
        binding.rlSex.setOnClickListener(view -> {
            String gender = binding.tvSex.getText().toString();
            setDialog(gender);
        });


    }

    private void setDialog(String gender) {
        int genderFlag = 0;
        if (gender.equals("保密")){
            genderFlag = 0;
        }else if (gender.equals("男")){
            genderFlag = 1;
        }else if (gender.equals("女")){
            genderFlag = 2;
        }
        String[] items = {"保密","男","女"};
        AlertDialog.Builder builder = new  AlertDialog.Builder(this);
        builder.setTitle("性别");//设置标题
        builder.setSingleChoiceItems(items,genderFlag, new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Toast.makeText(UserInfoActivity.this, items[which], Toast.LENGTH_SHORT).show();
                setSex(items[which]);
            }
        });
        builder.create().show();
    }
    public static ResultInfo resultInfo;
    private void postRequest(String gender, Integer id) {
        new Thread(() -> {
            Map<String, String> map = new HashMap<>();
            map.put("gender", gender);
            map.put("id", String.valueOf(id));
            String response = sendHttpRequest("/user/updateGender", map);
            Log.d("TAG", "postRequest: "+response);
            if(response!=null&&!response.isEmpty()){
                resultInfo = JSONObject.parseObject(response, ResultInfo.class);//将JSON字符串转换成对象
                if(resultInfo.isSuccess()){
                    user = resultInfo.getUser();
                    Log.d("TAG", "postRequest:sd22332 "+user);
                    LoginActivity.user.setGender(resultInfo.getUser().getGender());
                } else
                    runOnUiThread(() -> Toast.makeText(this, "修改失败", Toast.LENGTH_LONG).show());
            }
        }).start();
    }

    /**
     * 更新界面上的性别数据
     */
    private void setSex(String gender) {
        binding.tvSex.setText(gender);
        postRequest(gender,LoginActivity.user.getId());

    }

}